package com.fangyi.yunmusic.config;

import com.fangyi.yunmusic.interceptor.CommentNotificationInterceptor;
import com.fangyi.yunmusic.websocket.CommentNotificationHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{

    @Autowired
    private CommentNotificationHandler commentNotificationHandler;

//    @Bean
//    public ServerEndpointExporter serverEndpointExporter() {
//        return new ServerEndpointExporter();
//    }


    @Bean
    public CommentNotificationInterceptor commentNotificationInterceptor() {
        return new CommentNotificationInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        //注册处理拦截器,拦截url为socketServer的请求
        webSocketHandlerRegistry.addHandler(commentNotificationHandler, "/commentNotification").addInterceptors(commentNotificationInterceptor())
                .setAllowedOrigins("*");

    }
}